/**
 * @file childframehelp.h
 * @brief
 *
 * 
 *
 * @author 1823419256@qq.com
 * @version 1.0
 * @date 2011
*/

#ifndef MYBOOK_FRAME_CHILDFRAMEHELP_H_
#define MYBOOK_FRAME_CHILDFRAMEHELP_H_

#include "childframe.h"
#include "webcore/webcore.h"
#include "aspectfactory.h"

extern UINT WM_HELP_VIEWCREATED;
DWORD WINAPI _threadHelpCreateView(LPVOID lpData);

class CChildFrameHelp
	: public CWindowImpl<CChildFrameHelp, CChildFrame, CChildFrameTraits>
	, public CDoubleBufferImpl<CChildFrameHelp>
{
public:
	DECLARE_WND_CLASS(_T("MyChildFrameHelp"))

	CChildFrameHelp(CAspectAbstractFactory* pAspectFactory)
		: m_hChildView(NULL)
	{
		ATLASSERT(pAspectFactory);
	}

	HWND CreateFrame(HWND hParent, LPCTSTR lpszUrl)
	{
		m_strUrl = lpszUrl;

		CClientRect rect(hParent);
		return Create(hParent, rect);
	}

	void OnFinalMessage(HWND hWnd)
	{
		delete this;
	}

public:
	BEGIN_MSG_MAP(CChildFrameHelp)
		CHAIN_MSG_MAP(CDoubleBufferImpl<CChildFrameHelp>)
		MESSAGE_HANDLER(WM_CREATE, OnCreate)
		MESSAGE_HANDLER(WM_SIZE, OnSize)
		MESSAGE_HANDLER(WM_HELP_VIEWCREATED, OnToolsViewCreated)
	END_MSG_MAP()

// Handler prototypes (uncomment arguments if needed):
//	LRESULT MessageHandler(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
//	LRESULT CommandHandler(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
//	LRESULT NotifyHandler(int /*idCtrl*/, LPNMHDR /*pnmh*/, BOOL& /*bHandled*/)

	LRESULT OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
	{
		DWORD dwThreadID;
		CloseHandle(CreateThread(NULL, 0, _threadHelpCreateView, (LPVOID)m_hWnd, NULL, &dwThreadID));

		return 0;
	}

	LRESULT OnSize(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& /*bHandled*/)
	{
		if (m_hChildView)
			::SetWindowPos(m_hChildView, NULL, 0, 0, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), SWP_NOZORDER);

		return 0;
	}

	void DoPaint(CDCHandle dc)
	{
	}

	LRESULT OnToolsViewCreated(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& /*bHandled*/)
	{
		m_hChildView = (HWND)lParam;

		CClientRect rcClient(m_hWnd);
		::PostMessage(m_hChildView, WM_SIZE, 0, MAKELPARAM(rcClient.Width(), rcClient.Height()));

		CString strUrl;
		if (_tcsnicmp(m_strUrl, _T("http://"), 7) == 0 ||
			_tcsnicmp(m_strUrl, _T("https://"), 8) == 0 ||
			_tcsnicmp(m_strUrl, _T("file://"), 7) == 0)
		{
			strUrl = m_strUrl;
		}
		else
		{
			strUrl.Format(_T("file:///%sdata/%s"), _Module.GetModulePath(), m_strUrl);
			strUrl.Replace(_T("\\"), _T("/"));
		}

		USES_CONVERSION_EX;
		CComBSTR bstrUrl = T2OLE_EX((LPTSTR)(LPCTSTR)strUrl, 0);
		SendMessage(m_hChildView, WM_MYWEB_NAVIGATE, 0, (LPARAM)(BSTR)bstrUrl);

		return 0;
	}

public:
	virtual void Accept(class CChildFrameInitVisitor* pVistor)
	{
		pVistor->FrameInitVisit(this);
	}

	virtual bool Equal(CChildFrameTypeVisitor* pVistor)
	{
		return pVistor->FrameTypeVisit(this);
	}

protected:
	HWND m_hChildView;
	CString m_strUrl;
};


// CHelpExternalDispatchAdapter
class CHelpExternalDispatchAdapter : public CExternalDispatch
{
public:
	CHelpExternalDispatchAdapter(CWebContainer* pWebContainer);
	~CHelpExternalDispatchAdapter();

public:
	STDMETHOD(GetIDsOfNames)(REFIID riid, LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId);
	STDMETHOD(Invoke)(DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS  *pDispParams, VARIANT  *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr);
};

// CHelpCustomSiteAdapter
class CHelpCustomSiteAdapter : public CCustomSite
{
public:
	CHelpCustomSiteAdapter(CWebContainer* pWebContainer);
	~CHelpCustomSiteAdapter();

public:
	STDMETHOD(GetExternal)(IDispatch **ppDispatch);
	STDMETHOD(QueryService)(REFGUID guidService, REFIID riid, void **ppvObject);
	STDMETHOD(ShowMessage)(HWND hwnd, LPOLESTR lpstrText, LPOLESTR lpstrCaption, DWORD dwType, LPOLESTR lpstrHelpFile, DWORD dwHelpContext, LRESULT *plResult);

public:
	CExternalDispatch *m_pExternalDispatch;
};

// CHelpWebEventSinkAdapter
class CHelpWebEventSinkAdapter : public CWebEventSink
{
public:
	CHelpWebEventSinkAdapter(CWebContainer* pWebContainer);
	~CHelpWebEventSinkAdapter();

public:
	virtual HRESULT OnNewWindow2(REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS* pDispParams, VARIANT* pVarResult, EXCEPINFO* pExcepInfo, UINT* puArgErr);
	virtual HRESULT OnNewWindow3(REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS* pDispParams, VARIANT* pVarResult, EXCEPINFO* pExcepInfo, UINT* puArgErr);
	virtual HRESULT OnBeforeNavigate2(REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS* pDispParams, VARIANT* pVarResult, EXCEPINFO* pExcepInfo, UINT* puArgErr);

protected:
	BOOL IsToppestPage(IDispatch* pDispatch);
	BOOL IsLocalSnbFile(LPCTSTR lpszUrl);
};

// CHelpWebControlExAdapter
class CHelpWebControlExAdapter : public CWebControlEx
{
public:
	CHelpWebControlExAdapter(CWebContainer* pWebContainer);
	~CHelpWebControlExAdapter();

public:
};

class CHelpAdapterCreator : public CIfaceAdapterCreator
{
public:
	virtual CCustomSite* GetCustomSite(CWebContainer* pWebContainer)
	{
		return new CHelpCustomSiteAdapter(pWebContainer);
	}

	virtual CWebEventSink* GetWebEventSink(CWebContainer* pWebContainer)
	{
		return new CHelpWebEventSinkAdapter(pWebContainer);
	}

	virtual CWebControlEx* GetWebControlEx(CWebContainer* pWebContainer)
	{
		return new CHelpWebControlExAdapter(pWebContainer);
	}
};


#endif // MYBOOK_FRAME_CHILDFRAMEHELP_H_